//
//  HESearchData.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/16.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HESearchData.h"
#import "HEHomeSearchModel.h"
#import "HESearchEidtModel.h"
#import "HESearchResultController.h"
#import "HEGroupSearchModel.h"
#import "HEHotKeyModel.h"

@interface HESearchData ()<ZQSearchViewDelegate>

@property (nonatomic, strong) ZQSearchViewController *searchVc;
@property (nonatomic, strong) HEHomeSearchModel *searchModel;
@property (nonatomic, strong) HESearchResultController *resVC;


@end

@implementation HESearchData

- (void)dealloc {
    DLog(@"")
}

- (void)toSearchVC {
    NSArray *hots = @[];
    _resVC = [HESearchResultController new];
    
    ZQSearchViewController *vc = [[ZQSearchViewController alloc] initSearchViewWithHotDatas:hots resultController: _resVC];
    _searchVc = vc;
    vc.delegate = self;
    [self.superVC.navigationController pushViewController:vc animated:YES];
    vc.searchStr = _searchStr;

    [self loadHotKey];
}

- (void)loadHotKey {
 
    [WOAFNetWorkingHandle POSTNoHUDWithBaseUrl:KServerUrl suffixUrl:KsearchKeyword parameters:@{} success:^(id  _Nonnull responseObject) {
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        
        NSArray <HEHotKeyModel *> *keys = [HEHotKeyModel mj_objectArrayWithKeyValuesArray:response.data];
        
        NSMutableArray *arr = @[].mutableCopy;
        for (HEHotKeyModel *hot in keys) {
            [arr addObject: IsNotNull(hot.keyword)];
        }
        
        [self.searchVc setHotDataSource: arr];
     } failure:^(NSError * _Nonnull error) {
    }];
}

// 文字联想
- (void)wordAssociate:(NSString *)keyString dataBlock:(void (^)(NSMutableArray *arr))block{
    
    NSString *url = [NSString stringWithFormat:@"%@%@", KsearchLookup, keyString];
    [WOAFNetWorkingHandle GETNoHUDWithBaseUrl:KServerUrl suffixUrl:url parameters:@{} success:^(id  _Nonnull responseObject) {
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        NSMutableArray *arr = [NSMutableArray array];
        for (NSString *title in response.data) {
            HESearchEidtModel *edit = [HESearchEidtModel new];
            edit.image = IMAGE_NAMED(@"default");
            edit.title = title;
            edit.editType = SearchEditTypeFuzzy;
            [arr addObject:edit];
        }
        
        if (arr.count == 0) {
            HESearchEidtModel *edit = [HESearchEidtModel new];
            edit.image = IMAGE_NAMED(@"default");
            edit.title = keyString;
            edit.editType = SearchEditTypeFuzzy;
            [arr addObject:edit];
        }
        
        block(arr);
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

// 搜索数据
- (void)loadSearchData:(NSString *)keyString dataBlock:(void (^)(NSMutableArray *arr))block{
    
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:keyString forKey:@"text"];
    [dict setValue:@"android" forKey:@"client"];

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Ksearch parameters:dict success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        self.searchModel = [HEHomeSearchModel mj_objectWithKeyValues: response.data];
        DLog(@"%@", self.searchModel.txt)
        NSMutableArray *groups =@[].mutableCopy;
        
        if (self.searchModel.KtxtObj.product.count > 0) {
            HEGroupSearchModel *group = [HEGroupSearchModel new];
            group.groupName = @"健康优惠";
            group.childs = self.searchModel.KtxtObj.product;
            group.cellId = @"HESearchGoodsCell";
            group.type = HEGroupSearchContentTypeGoods;
            [groups addObject:group];
        }
        
        if (self.searchModel.KtxtObj.menu.count > 0) {
            HEGroupSearchModel *group = [HEGroupSearchModel new];
            group.groupName = @"健康导航";
            group.childs = self.searchModel.KtxtObj.menu;
            group.cellId = @"HESearchPageCell";
            group.type = HEGroupSearchContentTypeMenu;
            [groups addObject:group];
        }
        
        if (self.searchModel.KtxtObj.info.count > 0) {
            HEGroupSearchModel *group = [HEGroupSearchModel new];
            group.groupName = @"健康资讯";
            group.childs = self.searchModel.KtxtObj.info;
            group.cellId = @"HESearchHealthCell";
            group.type = HEGroupSearchContentTypeHealthInfo;
            [groups addObject:group];
        }
        
        if (self.searchModel.KtxtObj.video.count > 0) {
            HEGroupSearchModel *group = [HEGroupSearchModel new];
            group.groupName = @"宣教视频";
            group.childs = self.searchModel.KtxtObj.video;
            group.cellId = @"HESearchHealthCell";
            group.type = HEGroupSearchContentTypeVideo;
            [groups addObject:group];
        }
        
        block(groups);
         
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - ZQSearchViewDelegate
// 搜索联想列表
- (void)searchEditViewRefreshWithKeyString:(NSString *)keyString DataBlock:(void (^)(id))block {
    [self wordAssociate:keyString dataBlock:^(NSMutableArray *arr) {
        block(arr);
    }];
}

// 搜索结果页
- (void)searchFuzzyResultWithKeyString:(NSString *)keyString Data:(id<ZQSearchData>)data resultController:(UIViewController *)resultController{
     
    [self loadSearchData:keyString dataBlock:^(NSMutableArray *arr) {
        HESearchResultController * vc = (HESearchResultController *)resultController;
        [vc refreshData:arr];
    }];
}
  
// 跳转到具体页面
- (void)searchConfirmResultWithKeyString:(NSString *)keyString Data:(id<ZQSearchData>)data resultController:(UIViewController *)resultController {
 
}

@end
